import java.net.*;
import java.io.*;

public class HTTPUtills {

	public final static int HTTP_PORT = 80;    

	private DataInputStream in;        

	public String downloadWebPage(URL pageURL) {

		String host, file;
		InputStream pageStream = null;

		host = pageURL.getHost();
		file = pageURL.getFile();

		try {
			pageStream = getWebPageStream(host, file);
			if (pageStream == null) {
				return "";
			}
		} catch (Exception error) {
			System.out.println(error);
			return "";
		}

		DataInputStream in = new DataInputStream(pageStream);
		StringBuffer pageBuffer = new StringBuffer();
		String line;

		try {
			while ((line = in.readLine()) != null) {
				// System.out.println(line);
				pageBuffer.append(line);
			}
		} catch (Exception error) { }

		try {
			in.close();
		} catch (Exception ignored) {}

		return pageBuffer.toString();
	}

	public InputStream getWebPageStream (String host, String file)
			throws IOException, UnknownHostException {

		Socket          httpPipe;   
		InputStream     inn;        
		OutputStream    output;       
		PrintStream     out;        
		InetAddress     webServer;  

		webServer = InetAddress.getByName(host);

		httpPipe = new Socket(webServer, HTTP_PORT);
		if (httpPipe == null) {
			System.out.println("Can't create soket");
			return null;
		}

		inn  = httpPipe.getInputStream();    
		output = httpPipe.getOutputStream();

		in   = new DataInputStream(inn);     
		out  = new PrintStream(output);

		if (inn==null || output==null) {
			System.out.println("Can't open streams!");
			return null;
		}

		out.println("GET " + file + " HTTP/1.0\n");

		String response;
		while ( (response = in.readLine()).length() > 0 ) {
			// System.out.println(response);
		}

		return in; 
	}
} 
